package com.example.administrator.x2picturetransliteration.Base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;


import com.example.administrator.x2picturetransliteration.Bean.HttpDataBean;
import com.example.administrator.x2picturetransliteration.Home.Activity.BuyingMembersActivity;
import com.example.administrator.x2picturetransliteration.Http.DataManager;
import com.example.administrator.x2picturetransliteration.Util.MediaUtil;
import com.example.administrator.x2picturetransliteration.Util.ToastUtil;

import java.io.File;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Observer;
import rx.Subscription;

/**
 * Created by Administrator on 2019/5/6.
 */

public class HttpGo {

    private DataManager mDataManager;
    private Context context;

    public HttpGo(Context context){

        this.mDataManager = new DataManager();
        this.context = context;
    }

    public Subscription postSubscription(final String url, final Map<String,String> qqiuData, final HttpGoCallback mHttpGoCallback){

        final Observable<HttpDataBean> mObservable = mDataManager.postHttpData(url,qqiuData);

        return new BaseRx<HttpDataBean>(context,mObservable)
                .mObservable(new BaseRx.TheCallback<HttpDataBean>() {

                    private boolean judge = true;
                    @Override
                    public Observable<HttpDataBean> setObservable() {

                        if (judge){

                            return mObservable;
                        }else {

                            return mDataManager.postHttpData(url,qqiuData);
                        }

                    }
                }).mSubscribe(new Observer<HttpDataBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                        mHttpGoCallback.onError(e);
                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        codePd(httpDataBean,context,"");

                        mHttpGoCallback.onNext(httpDataBean);
                    }
                });

    }

    public Subscription postSubscription(final String url, final Map<String,String> qqiuData, final String msg, final HttpGoCallback mHttpGoCallback){

        final Observable<HttpDataBean> mObservable = mDataManager.postHttpData(url,qqiuData);

        return new BaseRx<HttpDataBean>(context,mObservable)
                .mObservable(new BaseRx.TheCallback<HttpDataBean>() {

                    private boolean judge = true;
                    @Override
                    public Observable<HttpDataBean> setObservable() {

                        if (judge){

                            return mObservable;
                        }else {

                            return mDataManager.postHttpData(url,qqiuData);
                        }

                    }
                }).mSubscribe(new Observer<HttpDataBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                        mHttpGoCallback.onError(e);
                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        codePd(httpDataBean,context,msg);

                        mHttpGoCallback.onNext(httpDataBean);
                    }
                });

    }

    public Subscription fileSubscription(final String url, final List<File> qqiuData, final HttpGoCallback mHttpGoCallback){

        final Observable<HttpDataBean> mObservable = mDataManager.postHttpFile(url,qqiuData);

        return new BaseRx<HttpDataBean>(context,mObservable)
                .mObservable(new BaseRx.TheCallback<HttpDataBean>() {

                    private boolean judge = true;
                    @Override
                    public Observable<HttpDataBean> setObservable() {

                        if (judge){

                            return mObservable;
                        }else {

                            return mDataManager.postHttpFile(url,qqiuData);
                        }

                    }
                }).mSubscribe(new Observer<HttpDataBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                        for (File file:qqiuData){
                            //file.delete();
                            MediaUtil.updateFileFromDatabase(context,file);
                        }
                        mHttpGoCallback.onError(e);
                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        for (File file:qqiuData){
                            //file.delete();
                            MediaUtil.updateFileFromDatabase(context,file);
                        }
                        codePd(httpDataBean,context,null);
                        mHttpGoCallback.onNext(httpDataBean);
                    }
                });

    }

    public Subscription fileSubscription(final String url, final File qqiuData, final HttpGoCallback mHttpGoCallback){

        final Observable<HttpDataBean> mObservable = mDataManager.postHttpFile(url,qqiuData);

        return new BaseRx<HttpDataBean>(context,mObservable)
                .mObservable(new BaseRx.TheCallback<HttpDataBean>() {

                    private boolean judge = true;
                    @Override
                    public Observable<HttpDataBean> setObservable() {

                        if (judge){

                            return mObservable;
                        }else {

                            return mDataManager.postHttpFile(url,qqiuData);
                        }

                    }
                }).mSubscribe(new Observer<HttpDataBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                        //qqiuData.delete();
                        MediaUtil.updateFileFromDatabase(context,qqiuData);
                        mHttpGoCallback.onError(e);
                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        //qqiuData.delete();
                        MediaUtil.updateFileFromDatabase(context,qqiuData);
                        codePd(httpDataBean,context,null);
                        mHttpGoCallback.onNext(httpDataBean);
                    }
                });

    }

    private void codePd(HttpDataBean httpDataBean,Context context,String msg){

        switch (httpDataBean.getCode()){

            case 1004:

                new ToastUtil(context).getToast(false,"需要先绑定手机号").show();
                break;

            case 1005:

                new ToastUtil(context).getToast(false,"请先购买VIP").show();
                context.startActivity(new Intent(context,BuyingMembersActivity.class));
                ((Activity)context).finish();
                break;

            case 2001:

                new ToastUtil(context).getToast(false,"用户名密码错误").show();
                break;

            case 2002:

                new ToastUtil(context).getToast(false,"账号已禁用,请联系管理员").show();
                break;

            case 1006:

                new ToastUtil(context).getToast(false,"文件过大").show();
                break;

            default:

                if (msg==null){

                    return;
                }else if (!"".equals(msg)){

                    new ToastUtil(context).getToast(false,msg).show();
                    return;
                }

                if (httpDataBean.getMsg()!=null && !"".equals(httpDataBean.getMsg()) && !"成功".equals(httpDataBean.getMsg())){

                    new ToastUtil(context).getToast(false,httpDataBean.getMsg()).show();
                }

        }
    }

    public interface HttpGoCallback{

        void onError(Throwable e);

        void onNext(HttpDataBean httpDataBean);
    }

}
